// pages/withdraw/withdraw.js
var utils = require('../../../utils/util.js');
var c = require('../../../utils/config.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
      defaultBank:[],
      bankId:0,
      money:0,
      tx_money:'',
  },
  onLoad: function (options) {
    this.setData({
      money:wx.getStorageSync("userinfo").money
    });
  },
   onShow:function(){
        this.getDefaultBank();
    },
    getDefaultBank:function(){//获取默认银行卡
        var that = this;
        var token = wx.getStorageSync("userinfo").wap_token;
        var data = {
            url: "/v1/userbank/getdefault",
            token: token,
        };
        utils.request(data).then(function (res) {
            if (res.code == 0) {
                var data = res.data;
                if (data.id == undefined){
                    that.setData({
                        defaultBank: data,
                        bankId: 0
                    });
                }else{
                    var str = data.bank_no;
                    data.bank_no = str.substr(str.length-4);
                    data.img_info.background_pic = c.img_url+data.img_info.background_pic;
                    if (data.type==0) {
                        data.type = '储蓄卡';
                    }else{
                        data.type = '银联';
                    }
                    that.setData({
                        defaultBank: data,
                        bankId: data.id
                    });
                }
            } else {

            }
        });
    },
    setmoney:function(e){
        this.setData({
            tx_money:e.detail.value
        });
    },
  setmoneyall:function(){
    this.setData({
      tx_money:wx.getStorageSync("userinfo").money
    });
  },
  addwithdraw:function(e){
    var that = this;
    if (this.data.bankId == 0 ){
        utils.errMsg("请先选择一个默认银行卡");
        return false;
    }
    var token = wx.getStorageSync("userinfo").wap_token;
    var bankid = this.data.bankId;
    var tx_money = this.data.tx_money;
    var data = {
      url: "/v1/withdraw/add",
      token: token,
      bank_id:bankid,
      money:tx_money
    };
    utils.request(data).then(function (res) {
        if (res.code == 0) {
            utils.updateUserinfo();
            wx.showToast({
                title: '提现成功',
                icon: 'none',
                duration: 1500
            });
            setTimeout(function(){
              wx.switchTab({
                  url: "/pages/my/usercenter/index",
              })
            },1000);
            
        }else{
            wx.showToast({
                title: res.msg,
                icon: 'none',
                duration: 1500
            }) 
        }
    });
  }
  

})
